package edu.ccut.saturn.studio.saturndata.wizard;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnBundleUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.database.model.DBConnection;
import edu.ccut.saturn.studio.database.model.DBConnectionProvider;

/**
 * 数据项的向导
 * 
 * @author ：赵新峰
 * @version 1.0 2008.12.25
 */
public class SaturnDataWizardsPage extends SaturnWizardPage {

	private static final String SATURNDATA_WZARD_DB_TYPE = "dbType"; //$NON-NLS-1$

	private static final String SATURNDATA_WIZARD_SHAPE_DESCRIPTION = "description"; //$NON-NLS-1$

	private static final String SATURNDATA_WIZARD_DB_CONNECTION = "dbConnection"; //$NON-NLS-1$

	private static final String SATURNDATA_WIZARD_NAME_SPACE = "nameSpace"; //$NON-NLS-1$

	public static final String SATURNDATA_WIZARD_TYPE_NAME = "dataTypeName"; //$NON-NLS-1$

	// 获得文件路径
	private Text txtDescription;
	private Combo cobDbConnection;
	private Text txtNameSpace;
	private Text txtDataType;

	public SaturnDataWizardsPage() {
		super("create saturn data"); //$NON-NLS-1$
		setTitle(Messages
				.getString("SaturnDataWizardsPage.createdatatypewizard")); //$NON-NLS-1$
		setDescription(Messages
				.getString("SaturnDataWizardsPage.descriptionlimit")); //$NON-NLS-1$
	}

	// 初始化map值
	public void initalValues(Map<String, Object> values) {
		values.put(SATURNDATA_WIZARD_TYPE_NAME, this.txtDataType.getText()
				.trim()); //$NON-NLS-1$
		values.put(SATURNDATA_WIZARD_NAME_SPACE, this.txtNameSpace.getText()
				.trim()); //$NON-NLS-1$
		values.put(SATURNDATA_WIZARD_DB_CONNECTION, this.cobDbConnection
				.getText().trim()); //$NON-NLS-1$
		values.put(SATURNDATA_WIZARD_SHAPE_DESCRIPTION, this.txtDescription
				.getText().trim()); //$NON-NLS-1$
		// 指定dbtype
		DBConnection conn = (DBConnection) DBConnectionProvider.getInstance()
				.getConnection(this.cobDbConnection.getText().trim());
		values.put(SATURNDATA_WZARD_DB_TYPE, conn.getType().toLowerCase()); //$NON-NLS-1$
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		setTitle(Messages
				.getString("SaturnDataWizardsPage.createdatatypewizard")); //$NON-NLS-1$
		setDescription(Messages
				.getString("SaturnDataWizardsPage.descriptionlimit")); //$NON-NLS-1$
		container.setLayout(gridLayout);

		setControl(container);

		final Label lblDataType = new Label(container, SWT.NONE);
		lblDataType.setText(Messages
				.getString("SaturnDataWizardsPage.datatypename")); //$NON-NLS-1$

		txtDataType = new Text(container, SWT.BORDER);
		txtDataType.setTextLimit(50);
		final GridData gd_txtDataType = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		txtDataType.setLayoutData(gd_txtDataType);

		txtDataType.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				// txtDataType输入校验
				validate();
				// 命名空间
				IProject project = SaturnProjectUtil.getProject(getSelection());

				IResource resource = SaturnProjectUtil.getResource(getSelection());

				String packagename = resource.getProjectRelativePath()
						.toOSString();
				String packageName = packagename.substring(packagename
						.indexOf(File.separator) + 1);
				packageName = packageName.replace(File.separator, "/"); //$NON-NLS-1$ 

				try {
					String bundleSymbolicName = SaturnBundleUtil
							.getBundleSymbolicName(project);
					String spacename = "http://" + bundleSymbolicName + "/" + packageName; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					txtNameSpace.setText(spacename);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

		final Label lblNameSpace = new Label(container, SWT.NONE);
		lblNameSpace.setText(Messages
				.getString("SaturnDataWizardsPage.namespace")); //$NON-NLS-1$

		txtNameSpace = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtNameSpace = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		txtNameSpace.setLayoutData(gd_txtNameSpace);

		final Label lblDbConnection = new Label(container, SWT.NONE);
		lblDbConnection.setText(Messages
				.getString("SaturnDataWizardsPage.choosedatabaselink")); //$NON-NLS-1$

		cobDbConnection = new Combo(container, SWT.READ_ONLY);
		final GridData gd_txtDbConnection = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		// 监听cobDbConnection校验
		cobDbConnection.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		cobDbConnection.setLayoutData(gd_txtDbConnection);

		List<String> names = DBConnectionProvider.getInstance()
				.getAllDBConnectionNames();
		String[] congnectnamestr = new String[names.size()];
		names.toArray(congnectnamestr);
		cobDbConnection.setItems(congnectnamestr);

		final Label lblDescription = new Label(container, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false,
				false));
		lblDescription.setText(Messages
				.getString("SaturnDataWizardsPage.description")); //$NON-NLS-1$

		txtDescription = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		txtDescription.setTextLimit(200);
		final GridData gd_txtDescription = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_txtDescription.heightHint = 76;
		txtDescription.setLayoutData(gd_txtDescription);
	}

	// 输入值校验方法
	public boolean validate() {
		String name = txtDataType.getText().trim();
		String connectionName = cobDbConnection.getText().trim();

		setPageComplete(false);

		if (new File(getFullPath() + "/" + name + ".saturndata").exists()) {
			setErrorMessage(Messages.getString("SaturnDataWizardsPage.NameExist")); //$NON-NLS-1$
		} else if (SaturnValidation.isEmpty(name)) {
			setErrorMessage(Messages
					.getString("SaturnDataWizardsPage.datanamelimit")); //$NON-NLS-1$
		} else if (!SaturnValidation.isValidProgramStr(name)) {
			setErrorMessage(Messages.getString("SaturnDataWizardsPage.NameLimit"));  //$NON-NLS-1$
		} else if (SaturnValidation.isEmpty(connectionName)) {
			setErrorMessage(Messages
					.getString("SaturnDataWizardsPage.pleasechoosedatabaselink")); //$NON-NLS-1$
		} else {
			setErrorMessage(null);
			setPageComplete(true);
			return true;
		}

		return false;
	}
}
